package com.runrunfast.fxj.handler;

import com.runrunfast.fxj.vo.ResultVo;
import com.runrunfast.fxj.exception.FxjException;
import com.runrunfast.fxj.utils.ResultVOUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

/**
 * @author: 陈胤训
 * @create: 2018-08-08 17:04
 **/
@Slf4j
@ControllerAdvice
public class FxjExceptionHandler {

    @ExceptionHandler(value = FxjException.class)
    @ResponseBody
    public ResultVo handlerSellException(FxjException e) {
        return ResultVOUtil.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultVo handlerException(Exception e) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PrintStream pout = new PrintStream(out);
        e.printStackTrace(pout);
        String ret = new String(out.toByteArray());
        pout.close();
        try {
            out.close();
        } catch (Exception ex) {
        }
        log.info("错误信息为 : {}",ret);
        return ResultVOUtil.error(1, ret);
    }

}
